package cn.easybuy.web.pre;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.easybuy.entity.Comment;
import cn.easybuy.entity.Order_detail;
import cn.easybuy.service.BuycarService;
import cn.easybuy.service.CommentService;
import cn.easybuy.service.impl.BuycarServiceimpl;
import cn.easybuy.service.impl.CommentServiceimpl;


public class CommentServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String opr=request.getParameter("opr");
		CommentService commentService=new CommentServiceimpl();
		BuycarService buycarService=new BuycarServiceimpl();
		if("addComment".equals(opr)){
			String praise=request.getParameter("praise");
			String content=request.getParameter("content");
			String productId=request.getParameter("productId");
			String userName=request.getParameter("userName");
			Comment comment=new Comment();
			comment.setComment_content(content);
			comment.setComment_product_id(Integer.parseInt(productId));
			comment.setComment_author(userName);
			comment.setComment_url("peo1.jpg");
			comment.setPraise(Integer.parseInt(praise));
			try {
				int result=commentService.AddComment(comment);
				String json=JSON.toJSONString(result);
				out.print(json);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if("commentsProductId".equals(opr)){
			String productId=request.getParameter("productId");
			try {
				List<Comment> lists=commentService.commentsProductId(Integer.parseInt(productId));
				String json=JSON.toJSONString(lists);
				out.print(json);
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}else if("commentPanDuan".equals(opr)){
			String userId=request.getParameter("userId");
			List<Order_detail> lists=new ArrayList<Order_detail>();
			Order_detail order_detail=null;
			try {
				if(buycarService.getUserIdOrderId(Integer.parseInt(userId)).size()==0){
					lists=new ArrayList<Order_detail>();
				}else{
					for (int i = 0; i < buycarService.getUserIdOrderId(Integer.parseInt(userId)).size(); i++) {
						for (int j = 0; j < buycarService.getOrderIdProductId(buycarService.getUserIdOrderId(Integer.parseInt(userId)).get(i).getId()).size(); j++) {
							order_detail=new Order_detail();
							order_detail.setProductId(buycarService.getOrderIdProductId(buycarService.getUserIdOrderId(Integer.parseInt(userId)).get(i).getId()).get(j).getProductId());
							lists.add(order_detail);
						}
					}
				}
				String json=JSON.toJSONString(lists);
				out.print(json);
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		out.flush();
		out.close();
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		doGet(request, response);
	}
}
